import useCopy from '@/hooks/useCopy'
import { Popover } from 'antd'

type CopyInfo = {
  text: string
  title?: string
  children?: React.ReactNode
}

export default function Index(props: CopyInfo) {
  const { text, title, children } = props
  const [copy] = useCopy()
  const copyAccessKey = () => {
    copy(text)
  }

  const style = {
    cursor: 'pointer'
  }

  return (
    <Popover
      content={
        <div onClick={copyAccessKey} style={style}>
          {text}
        </div>
      }
      title={title ?? '复制信息'}
    >
      <p onClick={copyAccessKey} style={style}>
        {children ?? text}
      </p>
    </Popover>
  )
}

/**
 * 用于复制一些信息，若是没有信息则展示------
 * @param text 复制的消息
 * @param title 复制时的标题
 * @returns
 */
export const CopyInfoOrBlack = (props: CopyInfo) => {
  const { text, title, children } = props
  const flag =
    text != '' &&
    text != null &&
    text != undefined &&
    text != 'null' &&
    text != 'undefined' &&
    text != '0'
  return flag ? <Index text={text} title={title} children={children} /> : '------'
}
